package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.framework.util.DateUtils;
import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.HotelBill;
import com.pignest.hotel.definition.BillType;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Print;
import com.pignest.hotel.system.PrintLODOP;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class BillsVbox extends Vbox implements EventListener {

	/**
	 * 票据
	 */
	private static final long serialVersionUID = 8443320064858090273L;

	private Textbox qRoomName;
	
	private Textbox qName;
	
	private Textbox qOrderNo;
	
	private Listbox qIsPrint;
	
	private Listbox qBillType;
	
	private Datebox qSOpTime;
	
	private Datebox qEOpTime;
	
	private Paging pagBills;
	private Listbox billsListbox;
	@Override
	public void onEvent(Event arg0) throws Exception {
		// TODO Auto-generated method stub

	}
	public void onCreate() {
		qRoomName =(Textbox)getFellow("qRoomName");
		qName =(Textbox)getFellow("qName");
		qOrderNo=(Textbox)getFellow("qOrderNo");
		qIsPrint=(Listbox)getFellow("qIsPrint");
		qBillType=(Listbox)getFellow("qBillType");
		qSOpTime=(Datebox)getFellow("qSOpTime");
		qEOpTime=(Datebox)getFellow("qEOpTime");
		billsListbox=(Listbox)getFellow("billsListbox");
		pagBills =(Paging)getFellow("pagBills");
		final int PAGE_SIZE = pagBills.getPageSize();
		pagBills.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	query(start,PAGE_SIZE);
		      	}
		    	});
			query(0,PAGE_SIZE);
	}
	public void query(int start,int length){
		try{
		billsListbox.getItems().clear();
		String sName = (String) qName.getValue();
		String roomName =(String) qRoomName.getValue();
		String orderNo =(String) qOrderNo.getValue();
		String isPrint = (String) UIUtils.getSelectedValue(qIsPrint);
		String type =(String)UIUtils.getSelectedValue(qBillType);
		Integer iIsPrint =null;
		if(isPrint!=null){
			iIsPrint =Integer.valueOf(isPrint);
		}
		Date sTime = qSOpTime.getValue();
		Date eTime = qEOpTime.getValue();
		
		List<HotelBill> l =Services.getOtherBusiService().queryHotelBill(roomName, sName, orderNo, type, iIsPrint, sTime, eTime);
		if(l!=null&&l.size()>0){
		  pagBills.setPageSize(l.size());
		}
		List <HotelBill> bs =Services.getOtherBusiService().queryHotelBillPage(roomName, sName, orderNo, type, iIsPrint, sTime, eTime, start, length);
		int index =1;
		if(bs==null)
			return ;
		for(HotelBill b:bs)
		{
			Listitem listitem = new Listitem();
			listitem.setValue(b.getId());
			listitem.setParent(billsListbox);
			listitem.appendChild(new Listcell(String.valueOf(index)));
			listitem.appendChild(new Listcell(b.getRoomName()));
			listitem.appendChild(new Listcell(b.getRoomType()));
			listitem.appendChild(new Listcell(b.getName()));
			listitem.appendChild(new Listcell(b.getOrderNo()));
			listitem.appendChild(new Listcell(DateUtil.toString(b.getOpTime(), DateUtil.DEFAULT_DATETIME_FORMAT)));
			//listitem.appendChild(new Listcell(Utils.toPrice(b.getDeposit())));
			listitem.appendChild(new Listcell(b.getType2()));
			listitem.appendChild(new Listcell(b.getIsPrint2()));
			listitem.appendChild(new Listcell(Utils.toPrice(b.getRoomPrice())));
			index ++;
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void print(String billId,String print){
		HotelBill b = Services.getOtherBusiService().get(HotelBill.class, billId);
		if(b==null)
			return ;
		//PrintLODOP p =new PrintLODOP();
		if(b.getType().equals(BillType.CHECK.name()))
		{
			Print.printCheck(b,print);
			
		}else if(b.getType().equals(BillType.LIVING.name())){
			Print.printLiving(b, print);
		}else if(b.getType().equals(BillType.DEPOSIT.name())){
			Print.printComeOn(b, print);
		}else if(b.getType().equals(BillType.CHANGEROOM.name())){
			Print.printChange(b, print);
		}else if(b.getType().equals(BillType.PARTCHECK.name())){
			Print.printPartCheck(b, print);
		}else
			Print.printCancel(b, print);
		if(print.equals("0")){
			b.setPrintTime(new Date());
			b.setIsPrint(1);
			Services.getOtherBusiService().update(b);
		}
	}
	
}
